﻿using ML.Entity;
using ML.IBusiness;
using System.Web.Mvc;

namespace ML.Web.Controllers.merchant
{
    /// <summary>
    /// 商户结算控制器
    /// </summary>
    [Login]
    public class SettlementController : BaseController
    {
        private readonly ISettlementBusiness settlementBusiness;
        private readonly IMerchantBusiness merchantBusiness;
        private readonly ICapitalAccountBusiness capitalAccountBusiness;
        public SettlementController(ISettlementBusiness _settlementBusiness, IMerchantBusiness _merchantBusiness,
            ICapitalAccountBusiness _capitalAccountBusiness)
        {
            settlementBusiness = _settlementBusiness;
            merchantBusiness = _merchantBusiness;
            capitalAccountBusiness = _capitalAccountBusiness;
        }

        /// <summary>
        /// 商户结算管理页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 申请结算页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult MerchantSettlment()
        {
            string merchantId = UserHelper.GetCurrentUser().MerchantId;
            if (string.IsNullOrEmpty(merchantId))
            {
                var data = merchantBusiness.GetNoDelInfoByUserId(UserHelper.GetCurrentUserId());
                if (data.status_code == 200)
                {
                    //如果查询到内容，将其保存到Session中
                    UserInfoSession userInfoSession = UserHelper.GetCurrentUser();
                    userInfoSession.MerchantId = data.data.Id;
                    userInfoSession.MerchantName = data.data.Name;
                    UserHelper.SetSession(userInfoSession);
                    merchantId = data.data.Id;
                }
                else
                {
                    merchantId = "";
                }
            }

            //根据商户Id查询商户可提现的金额和总金额
            var capitalAccount = capitalAccountBusiness.GetByMerchantId(merchantId).data;
            if (capitalAccount != null)
            {
                ViewBag.Balance = capitalAccount.Balance;   //商户总金额（人民币）
                ViewBag.ResidueMoney = capitalAccount.Balance - capitalAccount.Withdrawed;    //商户剩余可提现金额(人民币)
            }
            else
            {
                ViewBag.Balance = 0;
                ViewBag.Balance = 0;
            }
            ViewBag.MerchantId = merchantId;
            ViewBag.MerchantName = UserHelper.GetCurrentUser().MerchantName;
            return View();
        }

        /// <summary>
        /// 根据查询条件查询商户结算信息
        /// </summary>
        /// <param name="settlementSearch">商户结算查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult List(SettlementSearch settlementSearch)
        {
            var data = settlementBusiness.List(settlementSearch);
            return JsonDate(data.data);
        }

        /// <summary>
        /// 根据查询条件查询商户结算信息
        /// </summary>
        /// <param name="settlementSearch">商户结算查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult ListMerchant(SettlementSearch settlementSearch)
        {
            var merchantId = UserHelper.GetCurrentUser().MerchantId;
            if (string.IsNullOrEmpty(merchantId))
            {
                merchantId = "-1";
            }
            ViewBag.MerchantId = merchantId;
            settlementSearch.merchantid_search = merchantId;
            var data = settlementBusiness.List(settlementSearch);
            return JsonDate(data.data);
        }

        /// <summary>
        /// 提现申请
        /// </summary>
        /// <param name="settlemententity">体现申请实体</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult ApplyWithdraw(SettlementEntity settlemententity)
        {
            settlemententity.MerchantId = UserHelper.GetCurrentUser().MerchantId;
            settlemententity.Creater = UserHelper.GetCurrentUser().UserName;
            var data = settlementBusiness.Add(settlemententity);
            return Json(data);
        }

        /// <summary>
        /// 更改商户结算状态
        /// </summary>
        /// <param name="Id">商户结算Id</param>
        /// <param name="Type">类别（2-已完成；3-失败；4-取消）</param>
        /// <param name="Remark">备注</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult UpdateSettlementStatus(string Id, int Type, string Remark)
        {
            var Optioner = UserHelper.GetCurrentUser().NickName;
            var data = settlementBusiness.UpdateSettlementStatus(Id, Type, Optioner, Remark);
            return Json(data);
        }
    }
}